package avicit.bdp.dms.das.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.service.dto.BdpColumnHistoryDTO;
import avicit.bdp.common.service.dto.BdpTableDTO;
import avicit.bdp.common.service.dto.BdpTableHistoryDTO;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dms.das.dao.BdpDasTableHistoryDao;
import avicit.platform6.api.system.ConvertColumnClient;
import avicit.platform6.api.system.impl.SystemConstant;
import avicit.platform6.commons.utils.BusinessUtil;
import avicit.platform6.core.rest.msg.QueryRespBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author yangpf
 */
@Transactional
@Service
public class BdpDasTableHistoryService extends BaseService<BdpDasTableHistoryDao, BdpTableHistoryDTO> {

    @Autowired
    private ConvertColumnClient convertColumnClient;

    @Autowired
    private BdpDasColumnHistoryService columnHistoryService;

    @Autowired
    private BdpDasTableService tableService;

    /**
     * 获取版本表最大版本号
     * @param tableId
     * @return
     */
    public Double getMaxVersion(String tableId) {
        return this.mapper.getMaxVersion(tableId);
    }

    /**
     * 分页查询模型版本
     * @param tableId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public QueryRespBean<BdpTableHistoryDTO> getPageList(String tableId, String keyWords, Integer pageNo,
                                                         Integer pageSize) {
        QueryRespBean<BdpTableHistoryDTO> queryRespBean = new QueryRespBean<>();
        if(keyWords.toLowerCase().startsWith("v")){
            keyWords = keyWords.substring(1);
        }
        PageHelper.startPage(pageNo, pageSize);

        Page<BdpTableHistoryDTO> page = this.mapper.getPageList(tableId ,keyWords);
        //查询当前版本的信息
        BdpTableDTO currentTable = tableService.selectByPrimaryKey(tableId);
        List<BdpTableHistoryDTO> tableHistoryList = page.getResult();
        if(CollectionUtils.isNotEmpty(tableHistoryList)){
            for (BdpTableHistoryDTO tableHistoryDTO : tableHistoryList) {
                String version = "v" + tableHistoryDTO.getTableVersion();
                if(currentTable.getTableVersion().equals(tableHistoryDTO.getTableVersion())){
                    version += "(当前版本)";
                }
                tableHistoryDTO.setTableVersion(version);
                BdpColumnHistoryDTO columnHistoryDTO = new BdpColumnHistoryDTO();
                columnHistoryDTO.setTableId(tableHistoryDTO.getId());
                List<BdpColumnHistoryDTO> columnHistoryList = columnHistoryService.selectList(columnHistoryDTO);
                tableHistoryDTO.setColumnHistoryList(columnHistoryList);
            }
        }
        try {
            valueConvert(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        queryRespBean.setResult(page);
        BdpLogUtil.log4Query("逻辑模型模块", "逻辑模型版本", queryRespBean);
        return queryRespBean;
    }

    private void valueConvert(Page<BdpTableHistoryDTO> page) {
        //循环组装请求数据
        Map<String, Set<String>> convertFormData = new HashMap<>(16);
        for (BdpTableHistoryDTO tableHistoryDTO : page) {
            BusinessUtil.createConvertSet(convertFormData, SystemConstant.USER, tableHistoryDTO.getCreatedBy());
        }
        if (convertFormData.size() > 0) {
            //获取请求结果
            Map<String, Map<String, String>> convertResultData = convertColumnClient.replace(convertFormData);
            //循环设置Alias或Name的值
            for (BdpTableHistoryDTO tableHistoryDTO : page) {
                tableHistoryDTO.setCreatedBy(BusinessUtil.convertFormat(convertResultData, SystemConstant.USER,
                        tableHistoryDTO.getCreatedBy()));
            }
        }
    }

    public BdpTableHistoryDTO getHistoryTableById(String id) {
        return this.mapper.getHistoryTableById(id);
    }
}